package com.zidian.study.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zidian.student.trucks.R;
import com.zidian.study.bean.UserStatisticsBean;
import com.zidian.study.globa.Constant;
import com.zidian.study.globa.ToolBarActivity;
import com.zidian.study.utils.GetUserInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by GongCheng on 2016/12/5.
 */

public class UserStatisticsActivity extends ToolBarActivity {
    @Bind(R.id.ask_number)
    TextView askNumber;
    @Bind(R.id.evaluate_number)
    TextView evaluateNumber;
    @Bind(R.id.answer_number)
    TextView answerNumber;
    @Bind(R.id.publish_activity_number)
    TextView publishActivityNumber;
    @Bind(R.id.answer_questionnaire_number)
    TextView answerQuestionnaireNumber;
    @Bind(R.id.user_normal_number_tv)
    TextView userNormalNumberTv;
    @Bind(R.id.user_late_number_tv)
    TextView userLateNumberTv;
    @Bind(R.id.user_leave_number_tv)
    TextView userLeaveNumberTv;
    @Bind(R.id.user_leave_early_number_tv)
    TextView userLeaveEarlyNumberTv;
    @Bind(R.id.user_truant_number_tv)
    TextView userTruantNumberTv;
    @Bind(R.id.year_selector_spinner)
    Spinner yearSelectorSpinner;
    @Bind(R.id.month_selector_spinner)
    Spinner monthSelectorSpinner;

    private StringBuffer time ;
    private List<String> years;
    private static final String[] MONTH = {"01", "02", "03", "04",
            "05", "06", "07", "08", "09", "10", "11", "12"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_left);
        setContentView(R.layout.activity_user_statistics);
        ButterKnife.bind(this);
        setTitle("用户统计");
        initView();
    }

    private void initView() {
        initSpinner();
        setData("2017-01");
    }

    private void setData(String time) {
        String url = Constant.URL_GOAL + "studentCount/selectStudent";
        Map<String, String> map = new HashMap<>();
        map.put("studentId", GetUserInfo.getStudentId(this));
        map.put("token", GetUserInfo.getToken(this));
        map.put("time" ,time);
        OkHttpUtils.post().url(url).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Snackbar.make(userLeaveNumberTv, "获取失败",Snackbar.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getInt("code") == 200) {
                        Gson gson = new Gson();
                        UserStatisticsBean model = gson.fromJson(response , UserStatisticsBean.class);
                        askNumber.setText("提问次数" + model.getData().getIssueAddNum() + "次");
                        evaluateNumber.setText("吱数" + model.getData().getAnswerEvaluateNum()+ "次");
                        answerNumber.setText("回答问题" + model.getData().getAnswerIssueNum()+ "次");
                        answerQuestionnaireNumber.setText("回答问卷" + model.getData().getAnswerQuestionnaireNum()+ "次");
                        publishActivityNumber.setText("发布活动" + model.getData().getActivityAddNum()+ "次");
                        userNormalNumberTv.setText(model.getData().getCountNum().getNum() + "次");
                        userLateNumberTv.setText(model.getData().getCountNum().getLateNum()+ "次");
                        userLeaveNumberTv.setText(model.getData().getCountNum().getLeaveNum()+ "次");
                        userTruantNumberTv.setText(model.getData().getCountNum().getTruantNum()+ "次");
                        userLeaveEarlyNumberTv.setText(model.getData().getCountNum().getLeaveEarlyNum()+ "次");
                    } else {
                        Snackbar.make(userLeaveNumberTv, jsonObject.getString("message"),Snackbar.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void initSpinner() {
        time = new StringBuffer();
        years = new ArrayList<>();
        years.add("2017");

        final String[] year = new String[1];
        final String[] month = new String[1];

        ArrayAdapter yearAdapter = new ArrayAdapter(this, R.layout.spinner_item, years);
        yearAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
        yearSelectorSpinner.setAdapter(yearAdapter);
        yearSelectorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                year[0] = years.get(position);
                time.setLength(0);
                time.append(year[0]).append("-").append(month[0]) ;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        ArrayAdapter monthAdapter = new ArrayAdapter(this, R.layout.spinner_item, MONTH);
        monthAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
        monthSelectorSpinner.setAdapter(monthAdapter);
        monthSelectorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                month[0] = MONTH[position];
                time.append(year[0]).append("-").append(month[0]) ;
                setData(time.toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }


}
